package com.huangdebiao.oa.controller;

import com.alibaba.fastjson.JSON;
import com.huangdebiao.oa.bean.BaseBean;

import javax.servlet.GenericServlet;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;
import java.util.Map;


public abstract class HDB_BaseController extends GenericServlet {

    protected void printData(boolean check, Object data, Integer code, ServletResponse response){
        String json="";
        if(check){
            Map<String,Object> map = BaseBean.success(data);
            json = JSON.toJSONString(map);
        }else{
            if(code == 500){
                json =JSON.toJSONString(BaseBean.failure(data));
            }else if(code == 404){
                json =JSON.toJSONString(BaseBean.notFound(data));
            }else if (code == 400){
                json =JSON.toJSONString(BaseBean.paramError(data));
            }
        }

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter writer = response.getWriter()) {
            writer.print(json);
            writer.flush();
        }catch (Exception exception){
            System.out.println(exception);
        }
    }

}
